package com.luolc.instantmessager.chat;
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2016-2017 Luolc
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;

import com.hyphenate.easeui.EaseConstant;
import com.hyphenate.util.EasyUtils;
import com.luolc.instantmessager.base.BaseActivity;
import com.luolc.instantmessager.home.HomeActivity;
import com.luolc.instantmessager.permission.PermissionsManager;

public class ChatActivity extends BaseActivity {
  private ChatFragment chatFragment;

  private String toChatUserId;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState == null
        || getSupportFragmentManager().findFragmentByTag("fragment") == null) {
      chatFragment = createChatFragment();
      if (getIntent().getExtras() != null) {
        toChatUserId = getIntent().getExtras().getString(EaseConstant.EXTRA_USER_ID);
      }
      getSupportFragmentManager()
          .beginTransaction()
          .add(android.R.id.content, chatFragment, "fragment")
          .commit();
    }
  }

  private ChatFragment createChatFragment() {
    final ChatFragment fragment = new ChatFragment();
    fragment.setArguments(getIntent().getExtras());
    return fragment;
  }

  @Override
  protected void onNewIntent(Intent intent) {
    final String userId = intent.getStringExtra(EaseConstant.EXTRA_USER_ID);
    if (toChatUserId.equals(userId)) {
      super.onNewIntent(intent);
    } else {
      startActivity(intent);
      finish();
    }
  }

  @Override
  public void onBackPressed() {
    chatFragment.onBackPressed();
    if (EasyUtils.isSingleActivity(this)) {
      final Intent intent = new Intent(this, HomeActivity.class);
      startActivity(intent);
    }
  }

  @Override
  public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                         @NonNull int[] grantResults) {
    PermissionsManager.getInstance().notifyPermissionsChange(permissions, grantResults);
  }
}
